# -*- coding: utf-8 -*-
"""
质量控制器 - 管理不同质量等级的渲染参数
"""

from enum import Enum
from typing import Tuple


class QualityLevel(Enum):
    """质量等级"""
    PREVIEW = "preview"  # 480p15, 快速
    STANDARD = "standard"  # 720p30, 平衡
    HIGH = "high"  # 1080p30, 高质量
    ULTRA = "ultra"  # 1080p60, 最高质量

    @property
    def resolution(self) -> Tuple[int, int]:
        """分辨率"""
        mapping = {
            self.PREVIEW: (854, 480),
            self.STANDARD: (1280, 720),
            self.HIGH: (1920, 1080),
            self.ULTRA: (1920, 1080),
        }
        return mapping[self]

    @property
    def fps(self) -> int:
        """帧率"""
        mapping = {
            self.PREVIEW: 15,
            self.STANDARD: 30,
            self.HIGH: 30,
            self.ULTRA: 60,
        }
        return mapping[self]

    @property
    def bitrate(self) -> str:
        """比特率"""
        mapping = {
            self.PREVIEW: "1000k",
            self.STANDARD: "3000k",
            self.HIGH: "8000k",
            self.ULTRA: "12000k",
        }
        return mapping[self]


class QualityController:
    """质量控制器"""

    def __init__(self, level: QualityLevel = QualityLevel.STANDARD):
        """
        初始化控制器

        Args:
            level: 质量等级
        """
        self.level = level

    @staticmethod
    def select_quality(target_duration: float, available_time: float) -> QualityLevel:
        """
        根据可用时间自动选择质量等级

        Args:
            target_duration: 目标视频时长
            available_time: 可用时间

        Returns:
            推荐的质量等级
        """
        ratio = available_time / target_duration if target_duration > 0 else 1.0

        if ratio < 1.5:
            return QualityLevel.PREVIEW
        elif ratio < 3.0:
            return QualityLevel.STANDARD
        elif ratio < 6.0:
            return QualityLevel.HIGH
        else:
            return QualityLevel.ULTRA

    def get_resolution_string(self) -> str:
        """获取分辨率字符串"""
        w, h = self.level.resolution
        return f"{w}x{h}"

    def get_fps(self) -> int:
        """获取帧率"""
        return self.level.fps

    def get_bitrate(self) -> str:
        """获取比特率"""
        return self.level.bitrate
